<?php
/**
 * Created by PhpStorm.
 * User: peter
 * Date: 2018/6/27
 * Time: 上午9:37
 */

namespace app\mall\logic;


use app\model\GoodsAttach;
use app\model\GoodsCollection;
use extend\Functions;
use extend\STATUS_CODE;

class Collection
{
    /**
     * @param int $uid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function goodsItem (int $uid): array
    {
        $model      =   new GoodsCollection();
        $result     = $model
            ->alias('ga')
            ->join('goods g', 'ga.goods_id = g.id')
            ->join('goods_attach gc', 'ga.goods_id = gc.goods_id')
            ->field('ga.id,ga.goods_id,ga.thumb,ga.title,g.status,g.price,gc.views')
            ->where('ga.uid', $uid)
            ->order('g.status desc,ga.create_at desc')
            ->select();
        return Functions::result(
            STATUS_CODE::SUCCESS,
            $result
        );
    }
    /**
     * @purpose 收藏商品
     * @param int $goods_id
     * @param int $uid
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function goods(int $goods_id, int $uid): array
    {
        $goods          = new  \app\model\Goods();
        $collection     = new GoodsCollection();
        $goods_info     = $goods
            ->where('id', $goods_id)
            ->field('title,thumb')
            ->find();

        $data               = [];
        $data['uid']        = $uid;
        $data['goods_id']   = $goods_id;
        $data['thumb']      = $goods_info->thumb;
        $data['title']      = $goods_info->title;
        $result             = $collection
            ->isUpdate(false)
            ->save($data);
        if ($result) {
            (new GoodsAttach())
                ->where('goods_id', $goods_id)
                ->setInc('collection', 1);
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '收藏成功'
            );
        }
        return Functions::result(
            STATUS_CODE::CREATE_FAIL,
            '收藏失败!'
        );
    }

    /**
     * @purpose 取消商品收藏
     * @param int $uid
     * @param array $goods_id
     * @return array
     * @throws \Exception
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function unGoods (array $goods_ids,int $uid): array
    {
        $collection     = new GoodsCollection();
        $goodsAttach    = new GoodsAttach();
        $collection->startTrans();
        $result         = $collection
            ->where([
                'uid'       => $uid,
                'goods_id'  => $goods_ids
            ])
            ->delete();
        $dec            = $goodsAttach
            ->where(['goods_id' => $goods_ids])
            ->setDec('collection', 1);
        if ($result && $dec) {
            $collection->commit();
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '取消成功'
            );
        }
        $collection->rollback();
        return Functions::result(
            STATUS_CODE::DELETE_FAIL,
            '取消失败'
        );
    }
}